﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bio7
{
    using System.IO;

    /// <summary>
    ///  Represents instance of SBH problem.
    ///  It contains the sequence length and oligonucleotides.
    /// </summary>
    public class Instance
    {
        public int N { get; set; }

        public List<Olig> Oligs { get; private set; }
        public Instance()
        {
            Oligs = new List<Olig>();
        }

        public void ReadFile(string filePath)
        {
            using (var reader = new StreamReader(filePath))
            {
                N = int.Parse(reader.ReadLine());
                string line;
	            while ((line = reader.ReadLine()) != null)
	            {
		            this.Oligs.Add(new Olig(line)); // Add to list.
	            }
            }
        }

        public string GetSolution( List<int> solution)
		{
            String result = "";
	        var old_olig = Oligs[solution[0]];
	        var newOlig = Oligs[solution[0]];

	        result+=newOlig.Sequence;

            for(int i = 1; i < solution.Count; i++){
		        old_olig = newOlig;
                newOlig = Oligs[solution[i]];
		        result+=newOlig.Sequence.Substring(old_olig.Sequence.Length - old_olig.Distance(newOlig));
            }
	        return result;
		}

        public void PrintSolution( List<int> solution)
        {
            for (int i = 0; i < solution.Count; i++)
            {
                string o = Oligs[solution[i]].Sequence;
                if (i == 0)
                {
                    Console.Write("{0}", o);
                }
                else
                {
                    Console.Write("{0}", o[o.Length - 1]);
                }
            }
        }

        public void RemoveSolutionNodes( List<int> solution)
        {
            solution = solution.OrderBy(o => o).ToList();
            solution.Reverse();

            for(int i = 0; i < solution.Count; i++)
            {
                Oligs.Remove(Oligs[solution[i]]);
            }
        }
    }
}
